﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


public partial class Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            ItemsGet();
        }

         Incorrect_Format.Visible = false;
         string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
         SqlConnection Conn = new SqlConnection(connectionString);
         SqlDataReader reader = null;
         SqlCommand cmd = new SqlCommand("SELECT * FROM Arabic_News ORDER BY News_Date DESC", Conn);
         Conn.Open();
         reader = cmd.ExecuteReader();
         reader.Read();
         Label1.Text = reader[1].ToString();
         ImpNews_Image.ImageUrl = reader[4].ToString();
         reader.Close();
         Conn.Close();
    }


    public int CurrentPage
    {
        get
        {
            // look for current page in ViewState
            object o = this.ViewState["_CurrentPage"];
            if (o == null)
                return 0;	// default to showing the first page
            else
                return (int)o;
        }

        set
        {
            this.ViewState["_CurrentPage"] = value;
        }
    }

    private void ItemsGet()
    {
        // Read sample item info from XML document into a DataSet

        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        using (SqlConnection Conn = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM [Seminars]", Conn) { Connection = Conn };
            using (DataSet ds = new DataSet())
            {
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds);
                // Populate the repeater control with the Items DataSet
                PagedDataSource objPds = new PagedDataSource { DataSource = ds.Tables[0].DefaultView, AllowPaging = true, PageSize = 5, CurrentPageIndex = CurrentPage };
                lblCurrentPage.Text = String.Format("صفحة رقم: {0} من {1}", CurrentPage + 1, objPds.PageCount);
                // Disable Prev or Next buttons if necessary
                LnkPrev.Enabled = !objPds.IsFirstPage;
                LnkNext.Enabled = !objPds.IsLastPage;
                this.rptSeminars.DataSource = objPds;
            }
        }
        this.rptSeminars.DataBind();
    }

    protected void LnkPrev_Click(Object sender, EventArgs e)
    {
        // Set viewstate variable to the previous page
        CurrentPage -= 1;

        // Reload control
        ItemsGet();
    }
    protected void LnkNext_Click(Object sender, EventArgs e)
    {
        // Set viewstate variable to the next page
        CurrentPage += 1;

        // Reload control
        ItemsGet();
    }


    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        SqlConnection Conn = new SqlConnection(connectionString);
        string Input = Email.Text;
        if (check_Email(Input))
        {
            try
            {
                Conn.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO Subscribe VALUES(@Email)",Conn);
                cmd.Parameters.Add("@Email", Email.Text);
                cmd.ExecuteScalar();
                ClientScript.RegisterStartupScript(this.GetType(),"key","alert('تم حفظ البريد الإلكترونى')",true);
                Conn.Close();
                Email.Text = "";
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "key", "alert('لقد تم كتابة هذا البريد من قبل')", true);
            }
        }
    }

    bool check_Email(string E)
    {
        if (E.Contains("@"))
        {
            if (E.Contains(".com") || E.Contains(".COM"))
            {
                Incorrect_Format.Text = "Please write the e-mail with this format (name@webiste.com)";
                Incorrect_Format.ForeColor = System.Drawing.Color.Black;

                return true;
            }
        }
        Incorrect_Format.Text = "البريد الإلكترونى الذى ادخلته غير صحيح";
        Incorrect_Format.ForeColor = System.Drawing.Color.Red;
        Incorrect_Format.Visible = true;
        return false;
    }
}
